application = {};
application.quitFuncs = {};
require("platform.platform");
function application.run()

end

function application.addWidget(widget)
    if(application.widgets==nil)then
        application.widgets = {};
    end
    table.insert(application.widgets,widget);
end

if(ui:isPlatform("SDL2"))then
application.eventData = SDL_calloc(0x200,1);
application.eventData = toType(application.eventData,"SDL_Event");
application.eventTable = {};
application.eventTable["motion"] = application.eventData.motion;
application.eventTable["button"] = application.eventData.button;
function application.run()
    while(true)do
        if(application.quit)then
            break;
        end
        SDL_PollEvent(application.eventData);
        --local type = dr(application.eventData,0,4);
        type = application.eventData.type;
        if(type==SDL_QUIT)then
            application.quit = true;
        end
        application.event();
        if(application.widgets)then
            for k,v in ipairs(application.widgets)do
                if(v.render)then
                    --print(v.render);
                    v:render();
                end
            end
        end
        SDL_Delay(20);
    end
    cleanTextures();
    if(application.widgets)then
        for k,v in ipairs(application.widgets)do
            if(v.quit)then
                v:quit();
            end
        end
    end
    for k,v in pairs(application.quitFuncs) do
        v();
    end
end
local mousebutton = {
    "left",
    "middle",
    "right",
    "x1",
    "x2",
}
local eventTable = {
    [SDL_MOUSEMOTION] = function(e)
        if(application.motion==nil)then
            application.motion = {};
        end
        event = application.motion;
        e = e.motion;
        event.name = "mousemotion";
        event.x = e.x;
        event.y = e.y;
        return event;
    end,
    [SDL_MOUSEBUTTONDOWN] = function(e)
        if(application.button==nil)then
            application.button = {};
        end
        event = application.button;
        e = e.button;
        event.x = e.x;
        event.y = e.y;
        event.clicks = e.clicks;
        event.name = 'mousebuttondown';
        local v = e.button;
        for k=1,#mousebutton do
            if((v&1)~=0)then
                event[mousebutton[k]] = true;
            else
                event[mousebutton[k]] = nil;
            end
            v = v >> 1;
        end
        return event;
    end,
    [SDL_MOUSEBUTTONUP] = function(e)
        if(application.button==nil)then
            application.button = {};
        end
        event = application.button;
        e = e.button;
        event.x = e.x;
        event.y = e.y;
        event.clicks = e.clicks;
        event.name = 'mousebuttonup';
        local v = e.button;
        for k=1,#mousebutton do
            if((v&1)~=0)then
                event[mousebutton[k]] = true;
            else
                event[mousebutton[k]] = nil;
            end
            v = v >> 1;
        end
        return event;
    end
}

function application.event()
    local f = eventTable[application.eventData.type];
    if(f)then
        local event = f(application.eventTable);
        for k,v in ipairs(application.widgets)do
            if(v.event)then
                v:event(event);
            end
        end
    end
    return nil;
end

end

